package com.ym.ganio.gan;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ym.ganio.R;
import com.ym.ganio.bean.ArticleItem;
import com.yumo.common.ui.RecyclerViewItemClickListener;
import java.util.List;


/**
 * Created by yumodev on 17/6/5.
 * 文章列表适配器
 */

public class GanAdapter extends BaseRecyclerAdapter<RecyclerView.ViewHolder>{
    private final String LOG_TAG = "GanAdapter";
    public static int TYPE_ITEM = 2;
    public static int TYPE_ITEM_IMAGE = 3;
    private Context mContext = null;
    private List<ArticleItem> mDataList;
    private RecyclerViewItemClickListener mItemClickListener;

    public GanAdapter(Context context){
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM){
            return new ItemViewHolder(View.inflate(mContext, R.layout.gan_list_item, null), viewType);
        }else if (viewType == TYPE_ITEM_IMAGE){
            return new ItemViewHolder(View.inflate(mContext, R.layout.gan_list_item_image, null), viewType);
        }else{
            return super.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder){
            ItemViewHolder itemHolder = ItemViewHolder.class.cast(holder);
            final ArticleItem data = mDataList.get(position);
            itemHolder.mTitleView.setText(data.getDesc());
            if (itemHolder.mDateView != null){
                itemHolder.mDateView.setText(data.getPublishedAt().substring(0,10));
            }

            if (!TextUtils.isEmpty(data.getWho())){
                itemHolder.mWhoView.setText(data.getWho());
                itemHolder.mWhoView.setVisibility(View.VISIBLE);
            }else{
                itemHolder.mWhoView.setVisibility(View.GONE);
            }

            if (getItemViewType(position) == TYPE_ITEM){
                if (data.getImages() != null && data.getImages().size() > 0){
                    itemHolder.mImageView.setVisibility(View.VISIBLE);
                    String url = data.getImages().get(0) + "?imageView2/0/w/100";

                    Glide.with(mContext)
                            .load(url)
                            .into(itemHolder.mImageView);
                }else{
                    itemHolder.mImageView.setVisibility(View.GONE);
                }
            }else{
                itemHolder.mImageView.setVisibility(View.VISIBLE);
                String url = data.getUrl();

                Glide.with(mContext)
                        .load(url)
                        .into(itemHolder.mImageView);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    mItemClickListener.onClick(view, holder.getAdapterPosition());
                }
            });
        }

    }

    public void setItemClickListener(RecyclerViewItemClickListener listener){
        mItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        if (mDataList == null || mDataList.size() == 0){
            return 0;
        }
        return mDataList.size() + super.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
       if (hasHeader() && position == 0){
           return TYPE_HEADER;
       }

       if (hasFooter() && position == getItemCount() -1){
           return TYPE_FOOTER;
       }

       ArticleItem item = getItemData(position);
        if ("福利".equals(item.getType())){
            return TYPE_ITEM_IMAGE;
        }
       return TYPE_ITEM;
    }

    public ArticleItem getItemData(int position) {
        if (hasHeader() && hasFooter()){
            if (position == 0 || position == getItemCount() -1){
                return null;
            }else{
                return mDataList.get(position - 1);
            }
        }else if (hasHeader()){
            if (position == 0){
                return null;
            }else{
                return mDataList.get(position - 1);
            }
        }else if (hasFooter()){
            if (position < getItemCount() -1){
                return mDataList.get(position);
            }else{
                return null;
            }
        }else{
            return mDataList.get(position);
        }
    }

    public void setDataList(List<ArticleItem> dataList){
        mDataList = dataList;
    }

    public void addDataList(List<ArticleItem> dataList){
        if (mDataList == null){
            mDataList = dataList;
            return;
        }
        for (ArticleItem item : dataList) {
            boolean hasRepeat = false;
            for (ArticleItem hasItem: mDataList) {
                if (item.get_id().equals(hasItem.get_id())){
                    hasRepeat = true;
                }
            }

            if (!hasRepeat){
                mDataList.add(item);
            }
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder{

        public TextView mTitleView;
        public TextView mDateView;
        public ImageView mImageView;
        public TextView mWhoView;
        public ItemViewHolder(View itemView, int type) {
            super(itemView);
            if (type == TYPE_ITEM){
                mTitleView = (TextView) itemView.findViewById(R.id.title);
                mDateView = (TextView) itemView.findViewById(R.id.date);
                mImageView = (ImageView) itemView.findViewById(R.id.image);
                mWhoView = (TextView)itemView.findViewById(R.id.who);
            }else{
                mTitleView = (TextView) itemView.findViewById(R.id.title);
                mImageView = (ImageView) itemView.findViewById(R.id.image);
                mWhoView = (TextView)itemView.findViewById(R.id.who);
            }

        }
    }
}
